package cn.justforchris.user.service.impl;

import cn.justforchris.system.domain.Logininfo;
import cn.justforchris.system.mapper.LogininfoMapper;
import cn.justforchris.user.query.QueryPage;
import cn.justforchris.user.domain.User;
import cn.justforchris.user.mapper.UserMapper;
import cn.justforchris.user.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Slf4j
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void insertUser(User user) {
        userMapper.insertUser(user);
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(user.getUserName());
        logininfo.setType(1);
        logininfo.setPhone(user.getPhone());
        logininfo.setEmail(user.getEmail());
        logininfo.setSalt(user.getSalt());
        logininfo.setPassword(user.getPassword());

        logininfoMapper.insertLogininfo(logininfo);

    }

    @Override
    @Transactional
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void patchDelete(Long[] ids) {
        userMapper.patchDelete(ids);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateUser(user);
        Logininfo logininfo = new Logininfo();
        logininfo.setId(user.getId());
        logininfo.setUsername(user.getUserName());
        logininfo.setType(1);
        logininfo.setPhone(user.getPhone());
        logininfo.setEmail(user.getEmail());
        logininfo.setSalt(user.getSalt());
        logininfo.setPassword(user.getPassword());

        logininfoMapper.insertLogininfo(logininfo);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public PageInfo<User> queryPage(QueryPage queryPage) {
        PageHelper.startPage(queryPage.getCurrentPage(),queryPage.getPageSize());
        List<User> data = userMapper.queryPage(queryPage);
        /*
        这里获取到的是符合传入参数currentpage和pagesize的数据,也就是说,如果不传pagesize就只有五条因为怕个size如果不传入的话就是5
        但是如果有pagesize传入就根据传入数据来
        */
        return new PageInfo<>(data);
        /*这里面就包含了pagehelper帮忙计算出来的total(总共多少条,和根据分页提取出的数据)*/

    }


}
